package com.workcell.workdata.im.component.event;

import com.workcell.workdata.im.entity.EventMessage;
import com.workcell.workdata.im.entity.Message;
import org.springframework.context.ApplicationEvent;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class GroupEvent extends ApplicationEvent {

    private boolean replaceable = true;

    /**
     * 消息创建者ID
     */
    private final Long uid;

    private final List<Long> uidList = new LinkedList<>();

    private final List<Long> ignoreUidList = new LinkedList<>();

    public GroupEvent(Message message) {
        this(message,null);
    }

    public GroupEvent(Long groupId) {
        this(groupId,null);
    }

    public GroupEvent(Message message,Long uid) {
        super(message);
        this.uid = uid;
    }

    public GroupEvent(Long groupId,Long uid) {
        super(new EventMessage());
        getSource().setSender(groupId == null ? 0L : groupId);
        this.uid = uid;
    }

    public void setUidList(List<Long> uidList) {
        this.uidList.addAll(uidList);
    }

    public void addIgnored(long uid) {
        this.ignoreUidList.add(uid);
    }

    public void addIgnored(Collection<Long> uidList) {
        this.ignoreUidList.addAll(uidList);
    }

    @Override
    public Message getSource() {
        return (Message) source;
    }

    public List<Long> getUidList() {

        uidList.removeAll(ignoreUidList);

        return uidList;
    }

    public boolean isReplaceable() {
        return replaceable;
    }

    public void setReplaceable(boolean replaceable) {
        this.replaceable = replaceable;
    }

    public Long getUid() {
        return uid;
    }

    public void setAction(String action) {
        getSource().setAction(action);
    }

    public void setContent(String content) {
        getSource().setContent(content);
    }

    public void setExtra(String extra) {
        getSource().setExtra(extra);
    }
}
